﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using FunctionPoint.Services;
using Microsoft.VisualStudio.Shell;
using FunctionPoint.Common;
using FunctionPoint.Adornments;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using System.IO;

namespace FunctionPoint.Adornment
{
    /// <summary>
    /// Interaction logic for FunctionPointViewPortAdornmentControl.xaml
    /// </summary>
    public partial class FunctionPointViewPortAdornmentControl : UserControl
    {

        public static DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(FunctionPointViewPortAdornmentControl));

        public static DependencyProperty IsAdornmentExpandedProperty =
            DependencyProperty.Register("IsAdornmentExpanded", typeof(bool), typeof(FunctionPointViewPortAdornmentControl));

        public bool IsAdornmentExpanded
        {
            get
            {
                return (bool)GetValue(IsAdornmentExpandedProperty);
            }
            set
            {
                SetValue(IsAdornmentExpandedProperty, value);
            }
        }
        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {

                SetValue(TitleProperty, value);
            }
        }

        private EnvDTE80.DTE2 _dte2 = null;
        internal EnvDTE80.DTE2 DTE2
        {
            get
            {
                if (_dte2 == null)
                    _dte2 = ServiceProvider.GlobalProvider.GetService(typeof(SDTE)) as EnvDTE80.DTE2;

                return _dte2;
            }
        }

        private IFunctionPointManagerService _managementService = null;
        private IFunctionPointManagerService ManagementService
        {
            get
            {
                if (_managementService == null)
                    _managementService = ServiceProvider.GlobalProvider.GetService(typeof(SFunctionPointManagerService)) as IFunctionPointManagerService;

                return _managementService;
            }
        }

        //public FunctionPointViewPortAdornmentControl()
        //{
        //    InitializeComponent();
        //}

        public FunctionPointViewPortAdornmentControl(WorkItemAdornmentHost workItemAdornmentHost)
        {
            WorkItemAdornmentHost = workItemAdornmentHost;
            InitializeComponent();

        }
        public WorkItemAdornmentHost WorkItemAdornmentHost
        {
            get;
            set;
        }

        #region Drag and Drop Overrides

        protected override void OnDragEnter(DragEventArgs e)
        {
            Debug.WriteLine("ucFunctionPointViewPortAdornmentControl_OnDragEnter: {0}, {1}", e.RoutedEvent.Name, e.Handled);
            IsAdornmentExpanded = true;
            e.Handled = true;
            base.OnDragEnter(e);
        }
        protected override void OnDragLeave(DragEventArgs e)
        {
            Debug.WriteLine("ucFunctionPointViewPortAdornmentControl_OnDragLeave: {0}, {1}", e.RoutedEvent.Name, e.Handled);
            IsAdornmentExpanded = false;
            e.Handled = true;
            base.OnDragLeave(e);
        }
        protected override void OnDragOver(DragEventArgs e)
        {
            Debug.WriteLine("ucFunctionPointViewPortAdornmentControl_OnDragOver: {0}, {1}", e.RoutedEvent.Name, e.Handled);
            e.Handled = true;
            base.OnDragOver(e);
        }
        protected override void OnDrop(DragEventArgs e)
        {
            Debug.WriteLine("ucFunctionPointViewPortAdornmentControl_OnDrop: {0}, {1}", e.RoutedEvent.Name, e.Handled);
            //e.Handled = true;
            // letting the even fall through because otherwise it rips the text out.  not what we want.

            // probably will change a bit when we get the drop handler changed out some.  probably doing a sub control in here too
            WorkItem designPointItem = this.DataContext as WorkItem;

            if (designPointItem != null)
            {
                if (e.Data.GetDataPresent(Common.Constants.DROPHANDLER_DROPFORMAT_RICHTEXT))
                {
                    string snapShotData = e.Data.GetData(Common.Constants.DROPHANDLER_DROPFORMAT_RICHTEXT) as string;
                    var textViewSelection = this.WorkItemAdornmentHost.TextView.Selection.StreamSelectionSpan;

                    SnapshotSpan selectionSpan = textViewSelection.SnapshotSpan;
                    MarkupConverter.MarkupConverter converter = new MarkupConverter.MarkupConverter();
                    string currentDisplayName = ManagementService.GetCurrentUser().DisplayName;
                    string filePath = WorkItemAdornmentHost.TextView.GetFilePath();
                    int latestChangeset = ManagementService.GetLatestChangeSetId(filePath);
                    string htmlData = converter.ConvertRtfToHtml(snapShotData);
                    
                    CodeElementDetails codeElementDetails = new CodeElementDetails()
                    {
                        Class = string.Empty,
                        Method = string.Empty,
                        Namespace = string.Empty,
                        HtmlCode = htmlData
                    };


                    
                    ManagementService.CreateCodeImplementationWorkItem(designPointItem, currentDisplayName,
                         filePath, latestChangeset, selectionSpan, codeElementDetails);
                }
            }
            base.OnDrop(e);
        }

        protected override void OnGiveFeedback(GiveFeedbackEventArgs e)
        {
            Debug.WriteLine("ucFunctionPointViewPortAdornmentControl_OnGiveFeedback: {0}, {1}", e.RoutedEvent.Name, e.Handled);
            base.OnGiveFeedback(e);
        }

        protected override void OnQueryContinueDrag(QueryContinueDragEventArgs e)
        {
            Debug.WriteLine("ucFunctionPointViewPortAdornmentControl_OnQueryContinueDrag: {0}, {1}, Action:{2}", e.RoutedEvent.Name, e.Handled, e.Action);

            base.OnQueryContinueDrag(e);
        }

        #endregion

        #region Mouse Overrides


        protected override void OnMouseEnter(MouseEventArgs e)
        {
            Debug.WriteLine("Adornment_OnMouseEnter");
            IsAdornmentExpanded = true;
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            Debug.WriteLine("Adornment_OnMouseLeave");
            IsAdornmentExpanded = false;
            base.OnMouseLeave(e);
        }

        #endregion
    }
}
